package com.qubuyer.customview;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.text.TextPaint;
import android.util.AttributeSet;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

public class BoldTextView extends androidx.appcompat.widget.AppCompatTextView {

    Paint paint;
    TextView outLineTextView;

    public BoldTextView(@NonNull Context context) {
        this(context, null);
    }

    public BoldTextView(@NonNull Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public BoldTextView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        outLineTextView = new TextView(context, attrs, defStyleAttr);
        outLineTextView.setTextColor(Color.WHITE);
        TextPaint textPaint = outLineTextView.getPaint();
        textPaint.setStyle(Paint.Style.STROKE);
        textPaint.setStrokeWidth(10);
    }

    @Override
    public void setLayoutParams(ViewGroup.LayoutParams params) {
        super.setLayoutParams(params);
        outLineTextView.setLayoutParams(params);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        //设置轮廓文字
        CharSequence outlineText = outLineTextView.getText();

        if (outlineText == null || !outlineText.equals(getText())) {
            outLineTextView.setText(getText());
            postInvalidate();
        }
        outLineTextView.measure(widthMeasureSpec, heightMeasureSpec);
    }

    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        super.onLayout(changed, left, top, right, bottom);
        outLineTextView.layout(left, top, right, bottom);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        outLineTextView.draw(canvas);
        super.onDraw(canvas);
    }
}
